package com.compass.doctor.ui.mine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.ActivityHospitalizationRecordBinding
import com.compass.doctor.ui.home.adapter.HospitalizedLogAdapter
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.ui.mine.viewmodel.MineViewModel
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.utils.CheckNotifyPermissionUtils
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 住院证记录
 * Date:2023/12/21 20:46
 */
class HospitalizedLogActivity : BaseVBActivity<ActivityHospitalizationRecordBinding>(),
    OnRefreshListener,
    OnLoadMoreListener {

    private val mineViewModel: MineViewModel by viewModels()

    private lateinit var mAdapter: HospitalizedLogAdapter

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, HospitalizedLogActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var mPage = 1
    override fun initView(savedInstanceState: Bundle?) {
        mAdapter = HospitalizedLogAdapter()
        mBinding.apply {
            refreshLayout.apply {
                setEnableRefresh(true)
                setEnableLoadMore(true)
                setOnRefreshListener(this@HospitalizedLogActivity)
                setOnLoadMoreListener(this@HospitalizedLogActivity)
            }

            recyclerView.apply {
                layoutManager =
                    LinearLayoutManager(
                        this@HospitalizedLogActivity,
                        LinearLayoutManager.VERTICAL,
                        false
                    )
                val dp10 = dpToPx(10)
                addItemDecoration(NormalItemDecoration().apply {
                    setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                    setLastBottom(true)

                })
                adapter = mAdapter

            }
            imgClose.onClick {
                llOpenNotification.visibility = View.GONE
            }
            tvOpenNotification.onClick {
                CheckNotifyPermissionUtils.tryJumpNotifyPage(this@HospitalizedLogActivity)
            }

        }
        mAdapter.onItemClickListener={_, position ->
            val item=mAdapter.getItem(position)
            HospitalizedDetailsActivity.start(this@HospitalizedLogActivity,item?.id)

        }

    }

    override fun initData() {
        showLoading()
        getHospitalizedLog()
        mineViewModel.hospitalizedLogLiveData.observe(this){
            if (mPage==1){
                mAdapter.setData(it)
                mBinding.refreshLayout.finishRefresh()
                mBinding.refreshLayout.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding.emptyView.visible()
                } else {
                    mBinding.emptyView.gone()
                }
            }else{
                mBinding.refreshLayout.finishLoadMore()
                if (!it.isNullOrEmpty()){
                    mAdapter.addAll(it)
                }else{
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            }
            dismissLoading()
        }
    }

    /**
     * 获取推荐列表数据
     */
    private fun getHospitalizedLog() {
        mineViewModel.getHospitalizedLog(count = mPage)

    }


    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getHospitalizedLog()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getHospitalizedLog()
    }
}